<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .progress {
        border: solid;
        padding: 3px;
      }
      .progress > div {
        height: 10px;
        background: gray;
      }
    </style>
  </head>
  <body>
    <div class="progress">
      <div id="progressBar"></div>
    </div>
    <script>
      const progressBar = document.getElementById("progressBar");
      const xhr = new XMLHttpRequest();
      // 定义请求
      // POST http://localhost:3000/users HTTP/1.1
      xhr.open("GET", "http://127.0.0.1:3000/posts");

      xhr.onload = function () {
        // 当收到响应（报文）信息的时候执行
        // console.log(xhr.status); // 获取状态码
        // console.log(xhr.statusText); // 获取状态描述
        // console.log(xhr.response); // 获取报文主体
      };
      xhr.onerror = function () {
        // console.log("error");
      };
      xhr.onprogress = function (e) {
        progressBar.style.width = (e.loaded / e.total) * 100 + "%";
      };

      // 发送请求
      xhr.send();
    </script>
  </body>
</html>
